package com.woniuxy01;


import java.net.SocketTimeoutException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

/**
 * @author 作者
 * @create 2025-07-29 9:58
 */
public class Test02 {
    public static void main(String[] args) throws ParseException {
        Date date = new Date();
        System.out.println(date.getYear()+1900);
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.get(Calendar.YEAR));
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate.get(ChronoField.YEAR));
        System.out.println(localDate.getYear());
    }

    private static void test10() {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(dateTime);
//        dateTime=dateTime.plusMonths(1);
        dateTime = dateTime.plus(1, ChronoUnit.YEARS);
        System.out.println(dateTime);
    }

    private static void test09() {
        //String转LocalDateTime
        String strDateTime="2025-06-29 12:09:53";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(strDateTime, formatter);
        System.out.println(dateTime);
    }

    private static void test08() {
        //日期转字符串
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(dateTime);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String strDateTime = dateTime.format(formatter);
        System.out.println(strDateTime);
    }

    private static void test07() {
        LocalTime time = LocalTime.now();
        System.out.println(time);
        LocalDate date = LocalDate.now();
        System.out.println(date);
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(dateTime.toString());
    }

    private static void test06() {
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DATE));
        //日期计算:日期部分+1
        calendar.add(Calendar.DATE, 1);
        System.out.println(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DATE));
        //日期计算:年份+1
        calendar.add(Calendar.YEAR, 1);
        System.out.println(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DATE));
        calendar.add(Calendar.MONTH, -1);
        System.out.println(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DATE));
    }

    private static void test05() {
        //获取日历对象，底层是简单工厂，不是单例
        Calendar calendar = Calendar.getInstance();
        //设置，修改日期
        calendar.set(2000, 12, 1);
        //获取日期的部分信息
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
        System.out.println(calendar.get(Calendar.DATE));
        //设置日期的部分
        calendar.set(Calendar.YEAR, 2004);
        System.out.println(calendar.get(Calendar.YEAR));
    }

    private static void test04() throws ParseException {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入日期");
        String str = sc.nextLine();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = dateFormat.parse(str);
        System.out.println(date);
    }

    private static void test03() {
        Date date = new Date();
        System.out.println(date);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = dateFormat.format(date);
        System.out.println(strDate);
    }

    private static void test02() {
        Date date = new Date();
        System.out.println(date);
        date.setMonth(3);
        System.out.println(date);
        System.out.println(date.getYear());
        System.out.println(date.getYear() + 1900);
        System.out.println(date.getMonth());
        System.out.println(date.getDate());
    }

    private static void test01() {
        //无参构造，就是当前日期
        Date date1 = new Date();
        System.out.println(date1);

        //number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT
        Date date2 = new Date(1000);
        System.out.println(date2);

        Date date3 = new Date(System.currentTimeMillis());
        System.out.println(date3);

        Date date4 = new Date(1, 1, 1);
        System.out.println(date4);
    }
}
